package cn.com.gof.behavior.itertor;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: ConctreteMyAggregate</p>  
 * <p>Description: </p>  
 * @author WangJun
 * @date 2018年9月10日 上午9:37:08
 */
public class ConctreteMyAggregate {
	private List<Object> list = new ArrayList<>();

	public List<Object> getList() {
		return list;
	}

	public void setList(List<Object> list) {
		this.list = list;
	}
	
	public void add(Object o) {
		list.add(o);
	}
	public void remove(Object o) {
		list.remove(o);
	}
	public MyIterator createIterator() {
		return new ConctreteMyIterator();
	}
	private class ConctreteMyIterator implements MyIterator{
		private int cursor;
		@Override
		public boolean isFirst() {
			return cursor==0?true:false;
		}

		@Override
		public boolean isLast() {
			return cursor==(list.size()-1)?true:false;
		}

		@Override
		public Object next() {
			if(cursor < list.size()) {
				return list.get(cursor++);
			}			
			return list.get(cursor);
		}

		@Override
		public boolean hasNext() {
			return cursor < list.size() ? true:false;
		}

		@Override
		public void first() {
			cursor = 0;
		}

		@Override
		public void last() {
			cursor = list.size()-1;
		}
		
	}
}
